#!/bin/bash
ISO_VERSION=r4
DIR_APM_ISO=/root/apm_iso
DIR_RMPBUILD=/root/rpmbuild
DIR_CUSTOM_OS=/root/custom_os
DIR_SHARE_APM_M4=/data_apm/m4
DIR_SHARE_APM89=/mnt/89share/apm/V4
DIR_APM_C_DIR=C/APM_M4
DIR_APM_WEB_DIR=WEB
FILE_APM_ISO_ORG=CentOS-6.5_64_${ISO_VERSION}.iso
FILE_APM_DATE=`date +%Y%m%d%H`
FILE_APM_ISO_DEL=Broadview_APM_V6.0.R4
FILE_APM_ISO_DEST=${FILE_APM_ISO_DEL}_${FILE_APM_DATE}.iso

mk_rpm()
{
    local v_file_spec=$1
    local v_file_org=$2
    local v_file_dest=$3
    local v_file_build_rpm=$4
    local v_file_iso_rpm=$5
       
    echo -e "\nupdate ${v_file_build_rpm} ..."
    if [ ! -f ${v_file_org} ];  then
        echo "file not exist.  ${v_file_org} "
        exit 1;
    fi

    rm ${v_file_dest} ${v_file_build_rpm} -f
    cp ${v_file_org}  ${v_file_dest}

    rpmbuild -bb ${v_file_spec} >/dev/null 2>&1
    if [ ! -f ${v_file_build_rpm} ];  then
        echo "make rpm fail. ${v_file_build_rpm}"
        exit 1;
    fi

    rm -f ${v_file_iso_rpm}
    cp ${v_file_build_rpm} ${v_file_iso_rpm}
    if [ ! -f ${v_file_iso_rpm} ];  then
        echo "make rpm fail. ${v_file_iso_rpm}"
        exit 1;
    fi

    return 0;
}

# 清除不用rpm包
FILE_APM_RPM=apm_web-2*.rpm
rm -f ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

# 更新协议解码rpm包
FILE_APM_RPM=apm_broker-2.5.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}"/SPECS/apm_broker.spec" ${DIR_SHARE_APM_M4}/${DIR_APM_C_DIR}/Broadview_APM_M4.tar.gz ${DIR_RMPBUILD}/SOURCES/apm_broker_v2.5.0.tar.gz ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

FILE_APM_RPM=apm_console-3.0.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}/SPECS/apm_console.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-web_manager_console.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-console.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

FILE_APM_RPM=apm_collect-3.0.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}/SPECS/apm_collect.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-collect.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-collect.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

FILE_APM_RPM=apm_hub-3.0.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}/SPECS/apm_hub.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-hub.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-hub.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

FILE_APM_RPM=apm_das-3.0.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}/SPECS/apm_das.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-threshold_das.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-das.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

FILE_APM_RPM=elasticsearch-3.0.0-1.x86_64.rpm
mk_rpm ${DIR_RMPBUILD}/SPECS/elasticsearch.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-es.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-es.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

#FILE_APM_RPM=apm_server_init-3.0.0-1.x86_64.rpm
#mk_rpm ${DIR_RMPBUILD}/SPECS/apm_server_init.spec ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/apm.server-3.0.0-SNAPSHOT-server-init.zip ${DIR_RMPBUILD}/SOURCES/apm.server-3-init.zip ${DIR_RMPBUILD}/RPMS/x86_64/${FILE_APM_RPM} ${DIR_CUSTOM_OS}/iso/Packages/${FILE_APM_RPM}

#生成iso文件
echo -e "\nmake apm_iso ..."
cd ${DIR_CUSTOM_OS}
#sh ./mk_iso.sh debug
sh ./mk_iso.sh ${ISO_VERSION} ${FILE_APM_ISO_ORG}

if [ ! -f ${DIR_CUSTOM_OS}"/"${FILE_APM_ISO_ORG} ];  then
    echo "iso not exist. "${DIR_CUSTOM_OS}"/"${FILE_APM_ISO_ORG}
    exit 1;
fi

mkdir -p  ${DIR_SHARE_APM89}/${DIR_APM_C_DIR};rm -rf ${DIR_SHARE_APM89}/${DIR_APM_C_DIR}/*
mkdir -p  ${DIR_SHARE_APM89}/${DIR_APM_WEB_DIR};rm -rf ${DIR_SHARE_APM89}/${DIR_APM_WEB_DIR}/*
cp ${DIR_SHARE_APM_M4}/${DIR_APM_C_DIR}/* ${DIR_SHARE_APM89}/${DIR_APM_C_DIR}/.
cp ${DIR_SHARE_APM_M4}/${DIR_APM_WEB_DIR}/* ${DIR_SHARE_APM89}/${DIR_APM_WEB_DIR}/.

rm -f ${DIR_SHARE_APM89}/${FILE_APM_ISO_DEL}*.iso
cp ${DIR_CUSTOM_OS}/${FILE_APM_ISO_ORG} ${DIR_SHARE_APM89}/${FILE_APM_ISO_DEST}
echo "make apm iso ok. "${DIR_SHARE_APM89}"/"${FILE_APM_ISO_DEST}
